<?php
header('Content-Type: text/html; charset=utf-8');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ('./common/smtp/Exception.php');
require ('./common/smtp/PHPMailer.php');
require ('./common/smtp/SMTP.php');
function func_response($str = '')
{
    echo $str;exit;
}
function func_get($str){
    return isset($_GET[$str]) ? $_GET[$str] : '';
}
function func_request($str){
    return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
function func_post($str){
    return isset($_POST[$str]) ? $_POST[$str] : '';
}
function func_checkParams() {
    $sendEmail      = func_request('sendEmail');
    $sendEmailPwd   = func_request('sendEmailPwd');
    $sendName       = func_request('sendName');
    $sendTitle      = func_request('sendTitle');
    $toEmail        = func_request('toEmail');
    $template       = func_request('template');

    $host       = func_request('host');
    $port       = func_request('port');
    if (!func_isEmail($sendEmail)) {
        func_response('配置邮箱格式错误');
    }
    if (!$sendEmailPwd) {
        func_response('配置密码格式错误');
    }
    if (!func_isEmail($toEmail)) {
        func_response('发送邮箱格式错误');
    }
    return [$sendEmail,$sendEmailPwd,$sendName,$sendTitle, '',$toEmail, $template, $host, $port];
}
function func_randText() {
    $type = mt_rand(0,2);
    if ($type == 1) {
        $data = json_decode(file_get_contents("https://v1.hitokoto.cn/?encode=json"),true);
        if($data['hitokoto'] == '') {
            return func_randText();
        }
        return $data['hitokoto'];
    } else  if($type == 0){
        $res = file_get_contents("http://api.dsecret.com/yiyan/");
        if($res == '') {
            return func_randText();
        }
        return $res;
    } else {
        //$res = file_get_contents("https://www.yunboys.cn/yan/api.php");
        $res = file_get_contents("https://api.xiaolin.in/hitokoto/");
        if($res == '') {
            return func_randText();
        }
        return $res;
    }
}
function jsonp_decode($jsonp, $assoc = false) {
    if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
        $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    return json_decode(trim($jsonp,'();'), $assoc);
}
function func_getQQNick($qq = '') {
    $res = file_get_contents("https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?g_tk=1518561325&uins=".$qq);
    $nic = explode(',',$res);
    $name = trim(mb_convert_encoding($nic[6], "UTF-8", "GBK"),'"');
    //$name = jsonp_decode($res, true)[$qq][6];
    return $name ? $name : $qq;
}
function func_getQQLogo($qq = '', $spec = 100) {
    return 'http://q2.qlogo.cn/headimg_dl?dst_uin='. $qq .'&spec=' . $spec;
}
function func_isEmail($email) {
    $regex = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
    if (!preg_match($regex, $email)){
        return false;
    }
    return true;
}

function func_randStr($length = 8) {
    $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $len = strlen($str) - 1;
    $randstr = '';
    for ($i = 0; $i < $length; $i++) {
        $num = mt_rand(0, $len);
        $randstr .= $str[$num];
    }
    return $randstr;
}
function func_randNum() {
    return mt_rand(10000000,99999999);
}
function func_sendEmail($params = []) {
    $send_mail_name     = $params[0];
    $send_mail_pwd      = $params[1];
    $send_name          = $params[2];
    $send_title         = $params[3];
    $send_content       = $params[4];
    $accept_mail_name   = $params[5];

    $send_host   = empty($params[6]) ? 'smtp.mail.ru' : $params[6];
    $send_port   = empty($params[7]) ? 465 : $params[7];;
    $mail = new PHPMailer(true);
    try {
        //服务器配置
        $mail->CharSet ="UTF-8";                     //设定邮件编码
        $mail->SMTPDebug = 0;                        // 调试模式输出
        $mail->isSMTP();                             // 使用SMTP
        $mail->Host = $send_host;                // SMTP服务器
        $mail->SMTPAuth = true;                      // 允许 SMTP 认证
        $mail->Username = $send_mail_name;           // SMTP 用户名  即邮箱的用户名
        $mail->Password = $send_mail_pwd;             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
        $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
        $mail->Port = $send_port;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持

        $mail->setFrom($send_mail_name, $send_name);  //发件人
        $mail->addAddress($accept_mail_name);  // 收件人
        //$mail->addAddress('ellen@example.com');  // 可添加多个收件人
        $mail->addReplyTo($send_mail_name, $send_name); //回复的时候回复给哪个邮箱 建议和发件人一致
        //$mail->addCC('cc@example.com');                    //抄送
        //$mail->addBCC('bcc@example.com');                    //密送

        //发送附件
        // $mail->addAttachment('../xy.zip');         // 添加附件
        // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名

        //Content
        $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
        $mail->Subject = $send_title;
        $mail->Body    = $send_content;
        $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
        $mail->send();
        return true;
    } catch (Exception $e) {
        return 'error:'. $mail->ErrorInfo;
    }
}
